import { Component, Input, OnInit } from '@angular/core';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { HttpService } from "src/app/shared/service/http.service";
import { ModalController } from '@ionic/angular';
import { SharePageComponent } from '../share-page/share-page.component';
@Component({
  selector: 'app-recommand-banner',
  templateUrl: './recommand-banner.component.html',
  styleUrls: ['./recommand-banner.component.scss'],
})
export class RecommandBannerComponent implements OnInit {


  @Input() tagData: any;
  shareData: any;
  constructor(
    private appGlobalService: AppGlobalService,
    private modalController: ModalController,
    private httpService: HttpService) { }

  ngOnInit() { }


  gotoPage(obj) {
    try {
      this.share(obj);
      // if (obj && obj.type === 'share') {
      //   this.share(obj);
      // } else {
      //   if (obj) {
      //     const targetObj = { pageId: obj.target, params: obj.params, sessionId: localStorage.getItem('sessionId') };
      //     this.appGlobalService.goToForward(targetObj);
      //   }
      // }

    } catch (error) { }
  }

  share(link) {
    console.log('share --> page', link);
    // 请求参数
    const params = {
      sessionId: localStorage.getItem("sessionId"),
      pageId: link.target,
      params: link.params
    };
    this.httpService.post('application/page', params, res => {
      // console.log(JSON.parse(res.script));
      const fullObject = JSON.parse(res.script).views[0].tags[0];
      if (fullObject.type) {
        this.shareData = JSON.parse(res.script).views[0].tags[0];
      } else {
        this.shareData = JSON.parse(res.script).views[0].tags[0].data;
      }
      // console.log(this.shareData);
      this.presentModal();
    });
  }

  // 分享弹框
  async presentModal() {
    const modal = await this.modalController.create({
      showBackdrop: true,
      cssClass: 'sharePage',
      backdropDismiss: true,
      component: SharePageComponent,
      componentProps: { tagData: this.shareData }
    });
    await modal.present();
    // 监听销毁的事件
    const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
    console.log(modalData);
  }



}
